package com.ddstation.clinic.dpt.controller;

import com.ddstation.clinic.dpt.entity.DtClinicDepartmentEntity;
import com.ddstation.clinic.dpt.service.ClinicDptService;
import com.ddstation.crm.login.util.WnSessionUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 诊所科室
 * @author zhangdifeng
 * @date 2019年9月5日
 */
@Controller @Lazy
public class ClinicDptContoller {

	/**
	 * 初始化
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/wncrm_clinic_dpt.do")
	public String clinicDptEvent() {
		return "pc/clinic/clinic_dpt";
	}
	
	/**
	 * 加载数据
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/wncrm_clinic_dpt.do/load")
	public @ResponseBody Map<String, Object> clinicDptLoadEvent(HttpSession session) {
		String institutionId = WnSessionUtil.getInstitutionId(session);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("dpts", clinicDptService.get(institutionId));
		ret.put("errcode", 0);
		return ret;
	}
	
	
	/**
	 * 修改
	 * @return
	 */
	@RequestMapping(value = "/wncrm_clinic_dpt.do/saveOrUpdate")
	public @ResponseBody Map<String, Object> clinicDptSaveOrUpdateEvent(
			HttpSession session
			,@RequestParam String dptCd
			,@RequestParam String dptName
			,@RequestParam(required=false) String cityDptCd
			,@RequestParam(required=false) String provinceDptCd
			,@RequestParam(required=false) Integer orderNb
			,@RequestParam String sqlAction // "C：新增 U：更新"
			) {
		
		String institutionId = WnSessionUtil.getInstitutionId(session);
		
		Map<String, Object> ret = new HashMap<String, Object>();
		DtClinicDepartmentEntity dpt = new DtClinicDepartmentEntity();
		dpt.setInstitutionId(institutionId);
		dpt.setDptCd(dptCd);
		dpt.setDptName(dptName);
		dpt.setOrderNb(orderNb);
		dpt.setStatus(0);
		if ("C".equals(sqlAction.toUpperCase())) {
			if (clinicDptService.get(institutionId, dptCd) != null) {
				ret.put("errcode", 1); // 科室编号重复
				return ret;
			}
		}
		
		if (clinicDptService.saveOrUpdate(dpt) == 1) {
			ret.put("errcode", 0);
		} else {
			ret.put("errcode", -1);
		}
		return ret;
	}
	
	/**
	 * 删除
	 * @return
	 */
	@RequestMapping(value = "/wncrm_clinic_dpt.do/remove")
	public @ResponseBody Map<String, Object> clinicDptRemoveEvent(
			HttpSession session
			,@RequestParam String dptCd) {
		
		String institutionId = WnSessionUtil.getInstitutionId(session);
		
		Map<String, Object> ret = new HashMap<String, Object>();
		if (clinicDptService.containDoctor(institutionId, dptCd)) {
			ret.put("errcode", 1);
		} else {
			if (clinicDptService.remove(institutionId, dptCd) == 1) {
				ret.put("errcode", 0);
			} else {
				ret.put("errcode", -1);
			}
		}
		return ret;
	}
	
	@Inject private ClinicDptService clinicDptService = null;
}